/*
 * JavaScript file
 */
var PlaySongFlag = false;

$(function() {
	log("V2014/2/25");
	
	
	/* Button bind */
	// 事件处理函数：进入焦点&失去焦点common css
	$(".btn").bind("rotaryFocus", function() {
		$(this).addClass("focus");
	}).bind("rotaryBlur", function() {
		$(this).removeClass("focus");
	});

	// closeBtn 关闭按钮 select
	$(".closeBtn").bind("rotarySelect mouseup", function() {
		var self = this;
		$(self).addClass("selected");
		setTimeout(function() {
			$(self).removeClass("selected");
			gm.system.closeApp();
		}, 100);
	});

	var debugBoardHeight = 100;

	// debug 上翻页selected
	$("#prevBtn").bind("rotarySelect mouseup", function() {
		var self = this;
		$(self).addClass("selected");
		var top = parseInt($("#infoBoard").css("margin-top"));
		setTimeout(function() {
			$(self).removeClass("selected");
			if (top < 0) {
				top += debugBoardHeight;
				//jquery animate方法测试
				$("#infoBoard").animate({"margin-top": top + "px"},1000);
			}
		}, 100);
	});

	// debug 下翻页slected
	$("#nextBtn").bind("rotarySelect mouseup", function() {
		var self = this;
		$(self).addClass("selected");
		var debugHeight = $("#infoBoard").height();
		var top = parseInt($("#infoBoard").css("margin-top"));
		var hideHeight = debugHeight - debugBoardHeight + top;
		setTimeout(function() {
			$(self).removeClass("selected");
			if (hideHeight > 0) {
				top -= debugBoardHeight;
				$("#infoBoard").animate({"margin-top": top + "px"},1000);
			}
		}, 100);
	});
	
	// 启用音乐播放部分
	$("#songBtn").bind("rotarySelect mouseup",function(){
		var self = this;
		$(self).addClass("selected");
		setTimeout(function() {
			$(self).removeClass("selected");
			PlaySongFlag = true;
		}, 100);
	});
	
	// 确认关闭应用按钮
	$("#logoutBtn").bind("rotarySelect mouseup",function(){
		var self = this;
		$(self).addClass("selected");
		setTimeout(function() {
			$(self).removeClass("selected");
			gm.system.closeApp(); 
		}, 100);
	});
	
	// 取消应用关闭
	$("#cancelLogoutBtn").bind("rotarySelect mouseup",function(){
		var self = this;
		$(self).addClass("selected");
		setTimeout(function() {
			$("#logout").addClass("hidden");
			Rotary.fresh();
		}, 100);
	});
	
	
	var Workspace = Backbone.Router.extend({
		initialize : function() {
			CCR.Songs = new CCR.Models.Song();

			CCR.Player = new CCR.Models.Player();

			CCR.Favorites = new CCR.Models.Favorites();
			
			CCR.Customes = new CCR.Models.Customes();
			
			CCR.Lrc = new CCR.Models.Lrc();
			
			CCR.SongList = new CCR.Models.SongList();
		},

		routes : { "" : "index", // 默认页
		"index" : "index", // 首页
		"song/:id" : "songDetails", //切换歌曲
		"addFavoriteSong/:id" : "addFavoriteSong", //收藏歌曲
		"deleteFavoriteSong/:id" : "deleteFavoriteSong",	//取消收藏
		"isFavorite/:id" : "isFavorite",	//是否已收藏
		},

		songDetails : function(song_id) {
			CCR.SongInfoView = new CCR.Views.SongInfoView({// 单曲信息视图
				song_id : song_id
			});
		},

		index : function() {
			//初始化token
				CCR.Config.initToken(function(){
					//Rotary.init();
					//显示radio channel list view
					CCR.ChannelListView = new CCR.Views.ChannelListView();
					//CCR.HomeView.show();
				});
		},
		addFavoriteSong:function(song_id){
			CCR.Favorites.addFavSong(song_id,function(){
				log("歌曲已收藏 songId="+song_id);
				$("#favouriveBtn").html("取消收藏");
			});
		},
		deleteFavoriteSong:function(song_id){
			CCR.Favorites.deleteFavSong(song_id,function(){
				log("歌曲已取消收藏 songId="+song_id);
				$("#favouriveBtn").html("收藏");
			});
		},
		isFavorite:function(song_id){
			CCR.Favorites.isFavorite(song_id,function(is){
				if(is){
					//歌曲已收藏
					log("isFavorite : true");
				}else{
					//歌曲未收藏
					log("isFavorite : false");
				}
			});
		}
	});
	CCR.Router = new Workspace();
	Backbone.history.start();

	//

});